package thread;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.*;

/**
 * @author zxl
 * @version 1.0
 * @date 2021/10/26 15:51
 * 测试callable接口
 * 定义返回值
 * 可抛出异常
 */
public class testCallable {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        testCallable1 testCallable1 = new testCallable1("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201608%2F02%2F20160802001436_CtfiH.jpeg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1637827152&t=7c11aeed115b35ccb207912c83017496", "a1.jpg");
        testCallable1 testCallable2 = new testCallable1("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201608%2F02%2F20160802001436_CtfiH.jpeg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1637827152&t=7c11aeed115b35ccb207912c83017496", "a2.jpg");
        testCallable1 testCallable3 = new testCallable1("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201608%2F02%2F20160802001436_CtfiH.jpeg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1637827152&t=7c11aeed115b35ccb207912c83017496", "a3.jpg");

        //创建执行服务   目前不推荐使用 使用之定义线程池new ThreadPoolExecutor()
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        //提交执行
        Future<Boolean> submit1 = executorService.submit(testCallable1);
        Future<Boolean> submit2 = executorService.submit(testCallable2);
        Future<Boolean> submit3 = executorService.submit(testCallable3);
        //获取结果
        Boolean aBoolean1 = submit1.get();
        Boolean aBoolean2 = submit2.get();
        Boolean aBoolean3 = submit3.get();
        System.out.println(aBoolean1);
        System.out.println(aBoolean2);
        System.out.println(aBoolean3);
        //关闭服务
        executorService.shutdownNow();
    }
}

class testCallable1 implements Callable<Boolean> {

    String url;
    String filename;

    public testCallable1(String url, String filename) {
        this.url = url;
        this.filename = filename;
    }

    @Override
    public Boolean call() throws Exception {
        Boolean aBoolean = downImg(url,filename);
        if (aBoolean){
            System.out.println(filename + "下载完成");
            return true;
        }
        return false;
    }

    Boolean downImg(String url,String filename){
        String PATH = "D:\\JSP\\workspace\\study\\JavaFoundation\\";
        try {
            URL url1 = new URL(url);
            URLConnection urlConnection = url1.openConnection();
            InputStream inputStream = urlConnection.getInputStream();
            FileOutputStream fileOutputStream = new FileOutputStream(PATH + filename);
            byte[] bytes = new byte[1024];
            while (inputStream.read(bytes)!=-1){
                fileOutputStream.write(bytes,0,bytes.length);
            }
            fileOutputStream.close();
            inputStream.close();
            urlConnection.connect();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
